package kr.saeildamil.androidStudy_second.fragment.network;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainXML extends Activity {

	ProgressDialog progressDlg;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LinearLayout root = new LinearLayout(this);
		root.setOrientation(LinearLayout.VERTICAL);

		Button pullBtn = new Button(this);
		pullBtn.setOnClickListener(listener);
		pullBtn.setText("Pull parser");

		Button jsonBtn = new Button(this);
		jsonBtn.setOnClickListener(listener);
		jsonBtn.setText("Json parser");

		LayoutParams params = new LayoutParams(new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		root.addView(pullBtn, params);
		root.addView(jsonBtn, params);
		setContentView(root);

		new XmlJsonMake().execute();
	}

	private class XmlJsonMake extends AsyncTask<Void, Void, Void> {
		protected void onPreExecute() {
			super.onPreExecute();
			progressDlg = ProgressDialog.show(MainXML.this, "Wait",
					"order.xml & order.json Make");
		}

		protected Void doInBackground(Void... params) {
			FileOutputStream fos = null;
			try {
				fos = openFileOutput("order.xml", MODE_PRIVATE);
				String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
						+ "	<order>\n"
						+ "	<item Maker=\"Samsung\" price=\"23000\">Mouse</item>\n"
						+ "	<item Maker=\"LG\" price=\"12000\">Keyboard</item>\n"
						+ "	<item price=\"156000\" Maker=\"Western Digital\">HDD</item>\n"
						+ "	</order>";
				fos.write(xml.getBytes());
				fos = openFileOutput("order.json", MODE_PRIVATE);
				String json = "[\n"
						+ "	{ \"Product\":\"Mouse\", \"Maker\":\"Samsung\",  \"Price\":\"23000\",\n"
						+ " { \"Product\":\"Keyboard\", \"Maker\":\"LG\",  \"Price\":\"23000\",\n"
						+ " { \"Product\":\"HDD\", \"Maker\":\"Western Digital\",  \"Price\":\"156000\",\n"
						+ "]";
				fos.write(json.getBytes());
				Thread.sleep(3000);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return null;
		}

		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progressDlg.dismiss();
			Toast.makeText(MainXML.this, "order.xml & order.json Make",
					Toast.LENGTH_LONG).show();
		}

	}

	View.OnClickListener listener = new OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent();
			switch (v.getId()) {
			case 1:
				intent.setClass(MainXML.this, Pull_parserActivity.class);
				break;
			case 2:
				intent.setClass(MainXML.this, Json_parserActivity.class);
				break;
			}
			startActivity(intent);

		}
	};
}
